import axiosInstance from "../../plugins/axios";
import { ElMessage } from "element-plus";
import { ProjectDTO } from "../../type/Report/ProjectDTO";
import { Project, ProjectDTOs } from "../../type/Project";
import { User } from "../../type/User";
import { Role } from "../../type/Role";

export const getProjectInfo = async (pageNum: number, pageSize: number) => {
  try {
    const res = await axiosInstance.post(
      `/projects/page?pageNum=${pageNum}&pageSize=${pageSize}`
    );
    if (res.data.code == 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "获取项目信息失败");
      throw new Error(res.data.message || "获取项目信息失败");
    }
  } catch (error) {}
};

export const getProjectList = async () => {
  try {
    const res = await axiosInstance.get(`/projects/all`);
    if (res.data.code == 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "获取项目列表失败");
      throw new Error(res.data.message || "获取项目列表失败");
    }
  } catch (error) {}
};

export const searchProjectInfo = async (
  pageNum: number,
  pageSize: number,
  search: any
) => {
  try {
    const res = await axiosInstance.post("/projects/search", {
      pageNum,
      pageSize,
      search,
    });
    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "搜索任务信息失败");
      throw new Error(res.data.message || "搜索用户信息失败");
    }
  } catch (error) {}
};

export const findProjectById = async (tenantIds: string[]) => {
  try {
    const res = await axiosInstance.post(
      `/projects/findProjectById`,
      tenantIds
    );

    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "获取项目信息失败");
      throw new Error(res.data.message || "获取项目信息失败");
    }
  } catch (error) {}
};
export const findUserByProjectId = async (projectId: string): Promise<ProjectDTOs> => {
  try {
    const res = await axiosInstance.post("/projects/findUserByProjectId", {
      projectId,
    });

    if (res.data.code == 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "获取项目信息失败");
      throw new Error(res.data.message || "获取项目信息失败");
    }
  } catch (error) {
    throw new Error("获取项目信息失败");
  }
};
export const updataProjectState = async (projectId: string, status: string) => {
  const res = await axiosInstance.post(
    `/projects/updateStatus?projectId=${projectId}&status=${status}`
  );
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "更新项目状态失败");
    throw new Error(res.data.message || "更新项目状态失败");
  }
};
export const deleteProject = async (project: ProjectDTOs) => {
  try {
    const res = await axiosInstance.post("/projects/delete", project);
    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "删除项目信息失败");
      throw new Error(res.data.message || "删除项目信息失败");
    }
  } catch (error) {
    throw new Error("删除项目信息失败");
  }
};
export const deleteBatchProject = async (ids: string[]) => {
  try {
    const res = await axiosInstance.post("/projects/deleteBatch", ids);
    if (res.data.code === 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "删除项目信息失败");
      throw new Error(res.data.message || "删除项目信息失败");
    }
  } catch (error) {
    throw new Error("删除项目信息失败");
  }
};
export const importProjectInfo = async (formData: FormData) => {
  try {
    const res = await axiosInstance.post("/projects/import", formData, {
      headers: {
        "Content-Type": "multipart/form-data",
      },
    });
    if (res.data.code === 1) {
      ElMessage.success("导入项目成功！");

      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "导入项目信息失败");
      throw new Error(res.data.message || "导入项目信息失败");
    }
  } catch (error) {
    throw new Error("导入用户信息失败");
  }
};
export const exportProjectInfo = async () => {
  axiosInstance
    .get("/projects/export", {
      responseType: "blob", // 重要
    })
    .then((response) => {
      const url = window.URL.createObjectURL(new Blob([response.data]));
      const link = document.createElement("a");
      link.href = url;
      const contentDisposition = response.headers["content-disposition"];
      let fileName = "下载文件.xlsx";
      if (contentDisposition) {
        const fileNameMatch = contentDisposition.match(
          /filename\*=UTF-8''(.+)/
        );
        if (fileNameMatch.length === 2) {
          fileName = decodeURIComponent(fileNameMatch[1]);
        }
      }
      link.setAttribute("download", fileName);
      document.body.appendChild(link);
      link.click();
      link.remove();
    })
    .catch((error) => {
      ElMessage.error("下载失败，请重试");
      console.error("下载失败:", error);
    });
};
export const updateProject = async (project: Project) => {
  const res = await axiosInstance.post("/projects/update", project);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "更新项目信息失败");
    throw new Error(res.data.message || "更新项目信息失败");
  }
};
export const addProject = async (project: Project) => {
  const res = await axiosInstance.post("/projects/add", project);
  if (res.data.code === 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "添加项目信息失败");
    throw new Error(res.data.message || "添加项目信息失败");
  }
};
export const findUserByTenantId = async (project: Project): Promise<User[]> => {
  const res = await axiosInstance.post("/projects/findUserByTenantId", project);

  if (res.data.code == 1) {
    return res.data.data; // 返回数据
  } else {
    ElMessage.error(res.data.message || "获取项目信息失败");
    throw new Error(res.data.message || "获取项目信息失败");
  }
};

export const inviteUser = async (
  userList: User[],
  project: Project,
  roleId: string
) => {
  try {
    const res = await axiosInstance.post("/projects/inviteUser", {
      userList,
      project,
      roleId,
    });

    if (res.data.code == 1) {
      return res.data.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "邀请用户失败");
      throw new Error(res.data.message || "邀请用户失败");
    }
  } catch (error) {
    throw new Error("邀请用户失败");
  }
};
export const removeUser = async (user: User, project: Project, role: Role) => {
  try {
    const res = await axiosInstance.post("/projects/removeUser", {
      user,
      project,
      role,
    });

    if (res.data.code == 1) {
      return res.data; // 返回数据
    } else {
      ElMessage.error(res.data.message || "移除用户失败");
      throw new Error(res.data.message || "移除用户失败");
    }
  } catch (error) {
    throw new Error("移除用户失败");
  }
};
